package com.dishdex.menuhub.app.controller.user;

import com.dishdex.menuhub.app.domain.user.UserDetailVO;
import com.dishdex.menuhub.app.domain.user.UserLoginInfoVo;
import com.dishdex.menuhub.module.module.user.entity.User;
import com.dishdex.menuhub.module.module.user.service.UserService;
import com.dishdex.menuhub.module.utils.BaseUtils;
import com.dishdex.menuhub.module.utils.Response;
import com.dishdex.menuhub.module.utils.SignUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * ClassName:
 * Package: com.dishdex.menuhub.app.controller.user
 * Description:
 *
 * @Author:
 * @Create 2025/10/22 9:59
 * @Version 1.0
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/user/register")
    public Response userRegister(@RequestParam(value = "avatar", required = false) String avatar,
                                 @RequestParam(value = "phone", required = false) String phone,
                                 @RequestParam(value = "password", required = false) String password,
                                 @RequestParam(value = "name", required = false) String name) {
        Long result;
        try {
            result = userService.edit(null, avatar, phone, password, name);
        } catch (RuntimeException e) {
            UserLoginInfoVo userErrorVO = new UserLoginInfoVo();
            userErrorVO.setMessage(e.getMessage());
            return new Response(4004, userErrorVO);
        }
        UserLoginInfoVo userLoginInfoVo = new UserLoginInfoVo();
        userLoginInfoVo.setMessage("注册成功：" + "id--" + String.valueOf(result));
        return new Response(1001, userLoginInfoVo);
    }

    @RequestMapping("/user/login")
    public Response userLogin(@RequestParam(value = "phone", required = false) String phone,
                              @RequestParam(value = "password", required = false) String password) {
        Boolean result;
        try {
            result = userService.login(phone, password);
        } catch (RuntimeException e) {
            UserLoginInfoVo userErrorVO = new UserLoginInfoVo();
            userErrorVO.setMessage(e.getMessage());
            return new Response(4004, userErrorVO);
        }
        UserLoginInfoVo userLoginInfoVo = new UserLoginInfoVo();
        if (result) {
            userLoginInfoVo.setMessage("登录成功");
            User user = userService.getByPhone(phone);
            userLoginInfoVo.setSign(SignUtils.makeSign(user.getId()));
        } else {
            return new Response(1010);
        }
        return new Response(1001, userLoginInfoVo);
    }

    @RequestMapping("/user/info")
    public Response userInfo(@RequestParam(value = "userId", required = false) Long userId) {
        User user;
        try {
            user = userService.getById(userId);
        } catch (RuntimeException e) {
            UserDetailVO userErrorVO = new UserDetailVO();
            userErrorVO.setMessage(e.getMessage());
            return new Response(4004, userErrorVO);
        }
        UserDetailVO userDetailVO = new UserDetailVO()
                .setAvatar(user.getAvatar())
                .setPhone(user.getPhone())
                .setPassword(user.getPassword())
                .setName(user.getName())
                .setCreateTime(String.valueOf(user.getCreateTime()))
                .setUpdateTime(String.valueOf(user.getUpdateTime()));
        return new Response(1001, userDetailVO);
    }
}
